#include <REGX52.H>
#include <INTRINS.h>
sbit LCD_EN=P2^7;
sbit LCD_RS=P2^6;
sbit LCD_WR=P2^5;


#define LCD_DataPort P0

void LCD_Delay()		//@11.0592MHz 1ms
{
	unsigned char i, j;

	_nop_();
	i = 2;
	j = 199;
	do
	{
		while (--j);
	} while (--i);
}

void LCD_WriteCommand(unsigned char command)
{
	LCD_RS=0;
	LCD_WR=0;
	LCD_DataPort=command;
	LCD_EN=1;
	LCD_Delay();
	LCD_EN=0;
	LCD_Delay();
}
void LCD_WriteData(unsigned char datas)
{
	LCD_RS=1;
	LCD_WR=0;
	LCD_DataPort=datas;
	LCD_EN=1;
	LCD_Delay();
	LCD_EN=0;
	LCD_Delay();
}
void LCD_SetAC(unsigned char row,line)
{
	if(row==1)
	{
		LCD_WriteCommand(0x80|(line-1));
	}
	else if(row==2)
	{
		LCD_WriteCommand(0x80|(line-1)+0x40);
	}
}
void LCD_Init()
{
	LCD_WriteCommand(0x38);
	LCD_WriteCommand(0x0C);
	LCD_WriteCommand(0x06);
	LCD_WriteCommand(0x01);
}

void LCD_ShowChar(unsigned char row,line,charData)
{
	LCD_SetAC(row,line);
	LCD_WriteData(charData);
}
void LCD_ShowString(unsigned char row,line,unsigned char string[])
{
	unsigned char i;
	LCD_SetAC(row,line);
	for(i=0;string[i]!='\0';i++)
	{
		LCD_WriteData(string[i]);
	}
}
unsigned int Pow(int x,y)
{
	unsigned char i;
	int temp=1;
	for(i=0;i<y;i++)
	{
		temp*=x;
	}
	return temp;
}
void LCD_ShowNum(unsigned char row,line,unsigned int num,unsigned char length)
{
	unsigned char i;
	LCD_SetAC(row,line);
	for(i=length;i>0;i--)
	{
		LCD_WriteData('0'+num/Pow(10,i-1)%10);
	}
}
void LCD_ShowSignedNum(unsigned char row,line,int num,unsigned char length)
{
	unsigned char i;
	unsigned int num1;
	LCD_SetAC(row,line);
	if(num>=0)
	{
		LCD_WriteData('+');
		num1=num;
	}
	else
	{
		LCD_WriteData('-');
		num1=-num;
	}
	for(i=length;i>0;i--)
	{
		LCD_WriteData('0'+num1/Pow(10,i-1)%10);
	}
}
void LCD_ShowHexNum(unsigned char row,line,unsigned int num,unsigned char length)
{
	unsigned char i;
	unsigned char temp;
	LCD_SetAC(row,line);
	for(i=length;i>0;i--)
	{
		temp=num/Pow(16,i-1)%16;
		if(temp<10)
		{
			LCD_WriteData('0'+temp);
		}
		else
		{
			LCD_WriteData('A'+temp-10);
		}
	}
}
void LCD_ShowBinNum(unsigned char row,line,unsigned int num,unsigned char length)
{
	unsigned char i;
	LCD_SetAC(row,line);
	for(i=length;i>0;i--)
	{
		LCD_WriteData('0'+num/Pow(2,i-1)%2);
	}
}





